/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farmcode.plugin.objectinfo.datasource.types;

import com.farmcode.api.plugin.ConfigureException;
import com.farmcode.api.plugin.impl.DefaultObjectInfoPlugin;
import com.farmcode.api.runtime.FARMCodeContext;
import com.farmcode.api.runtime.metadata.Child;
import com.farmcode.api.runtime.metadata.ObjectInfo;
import com.farmcode.api.runtime.metadata.ObjectInfoSpc;
import com.farmcode.api.runtime.metadata.tables.Attribute;
import com.farmcode.plugin.objectinfo.datasource.FARMCodeDatabaseMetadata;
import com.farmcode.plugin.objectinfo.datasource.MetadataUtils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author oscar
 */
public class ObjectInfoLocalDatasourceTables extends DefaultObjectInfoPlugin {

    private Connection connection;      
    private FARMCodeDatabaseMetadata metadata;
    
    public ObjectInfoLocalDatasourceTables() {
    }

    @Override
    public List<ObjectInfo> getObjectsInfo(FARMCodeContext context, String objectPattern) {
        List<ObjectInfo> objectInfos = new ArrayList<ObjectInfo>();
        try {
            String schema = (String)context.get(FARMCodeContext.SCHEMA);
            metadata = new FARMCodeDatabaseMetadata(connection);
            ResultSet resultSet = metadata.getObject(schema, objectPattern, new String[]{"TABLE"});
             while (resultSet.next()) {
                 ObjectInfo objectInfo = new ObjectInfoSpc(schema, resultSet.getString("OBJECT_NAME"), "TABLE", false, "");                 
                 objectInfos.add(objectInfo);
             }
        } catch (SQLException ex) {
            Logger.getLogger(ObjectInfoLocalDatasourceTables.class.getName()).log(Level.SEVERE, null, ex);
        }
        return objectInfos;
    }

    @Override
    public ObjectInfo getObjectAttributes(FARMCodeContext context, ObjectInfo objectInfo) {
         
        try {
            metadata = new FARMCodeDatabaseMetadata(connection);
            ResultSet childs  = metadata.getObjectChilds(objectInfo.getSchema(), objectInfo.getName(), new String[]{"COLUMN"});
            
            while (childs.next()) {
                
                Attribute child = new Attribute(childs.getString("OBJECT_NAME"), "COLUMN");
                objectInfo.getChilds().add(child);
                     
            }
        } catch (SQLException ex) {
            Logger.getLogger(ObjectInfoLocalDatasourceTables.class.getName()).log(Level.SEVERE, null, ex);
        }
         
        return objectInfo;
    }
    
    @Override
    public void configure() throws ConfigureException {
        try {
            
            Class.forName(MetadataUtils.getStringBundle("config", "driver"));
            connection = DriverManager.getConnection
                (
                    MetadataUtils.getStringBundle("config", "host"), 
                    MetadataUtils.getStringBundle("config", "user"),
                    MetadataUtils.getStringBundle("config", "password")
                );
        } catch (SQLException ex) {
            Logger.getLogger(ObjectInfoLocalDatasourceTables.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ObjectInfoLocalDatasourceTables.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    
}
